﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// add
using System.IO;

namespace LibTCP
{
    public class CCommon
    {
        /// <summary>
        /// Input Line1=Len
        /// Input Line2=sHexFormatWithBitConverter="53-4F-45-50-53-00-00-00-9B-00..."
        /// </summary>
        /// <param name="sFile"></param>
        /// <returns></returns>
        public static bool LoadBuffer(string sFile, out byte[] baOutput)
        {
            string sHexFormatWithBitConverter, sLen;
            int iLen;

            sHexFormatWithBitConverter = string.Empty;
            sLen = string.Empty;
            using (StreamReader sr = new StreamReader(sFile))
            {
                String sLine;
                int iLine;

                iLine = 0;
                iLen = 0;
                sHexFormatWithBitConverter = string.Empty;
                while ((sLine = sr.ReadLine()) != null)
                {
                    iLine++;
                    switch (iLine)
                    {
                        case 1:
                            sLen = sLine;
                            break;
                        case 2:
                            sHexFormatWithBitConverter = sLine;
                            break;
                        default:
                            break;
                    }
                }
                int.TryParse(sLen, out iLen);
            }
            return ToBytes(sHexFormatWithBitConverter, iLen, out baOutput);
        }

        /// <summary>
        /// input: sHexFormatWithBitConverter="53-4F-45-50-53-00-00-00-9B-00..."
        /// </summary>
        /// <param name="sHexFormatWithBitConverter"></param>
        /// <param name="iBytes"></param>
        /// <returns></returns>
        public static bool ToBytes(string sHexFormatWithBitConverter, int iBytes, out byte[] baOutput)
        {
            //sHexFormatWithBitConverter="53-4F-45-50-53-00-00-00-9B-00..."
            byte bValue;
            string sValue;

            baOutput = null;
            if (iBytes < 1)
                return false;

            baOutput = new byte[iBytes];
            for (int i = 0; i < iBytes; i++)
            {
                sValue = sHexFormatWithBitConverter.Substring(i * 3, 2);
                if (HexStringToByte(sValue, out bValue))
                    baOutput[i] = bValue;
                else
                    return false;
            }
            return true;
        }
        public static byte[] ToBytes(int iInput)
        {
            return BitConverter.GetBytes(iInput);
        }


        /// <summary>
        /// input "A", return 10.
        /// </summary>
        /// <param name="sHex"></param>
        /// <param name="bOutput"></param>
        /// <returns></returns>
        public static bool HexStringToByte(string sHex, out byte bOutput)
        {
            return byte.TryParse(sHex, System.Globalization.NumberStyles.AllowHexSpecifier, null, out bOutput);


        }
        public static bool SaveBuffer(string sFile, byte[] ba, int iStart, int iLen)
        {
            StringBuilder sb = new StringBuilder();

            sb.Length = 0;
            //sb.AppendFormat("DumpBuffer(): length={0}.", iLen);
            sb.AppendLine(iLen.ToString());

            sb.AppendFormat("{0}", ToHex(ba, iStart, iLen));
            sb.AppendLine(".");

            for (int i = 0; i < iLen; i++)
                sb.AppendFormat("{0,-3}", ToASCIIPrintable(ba[i]));
            sb.AppendLine(".");

            for (int i = 0; i < iLen; i++)
                sb.AppendFormat("{0,-3}", i.ToString("X"));
            sb.AppendLine(".");
            for (int i = 0; i < iLen; i++)
                sb.AppendFormat("{0,-3}", i);
            sb.AppendLine(".");

            File.WriteAllText(sFile, sb.ToString());


            return true;
        }
        public static string ToHex(byte b1)
        {
            return string.Format("{0:X}", b1);
        }
        public static string ToHex(int i1)
        {
            return string.Format("{0:X}", i1);
        }
        public static string ToHex(byte[] ba1)
        {
            return BitConverter.ToString(ba1);
        }
        public static string ToHex(byte[] ba1, int iStart, int iLen)
        {
            if (iLen < 1)
                return string.Empty;
            if (ba1 == null)
                return string.Empty;
            return BitConverter.ToString(ba1, iStart, iLen);
        }
        public static string ToASCIIPrintable(int iInput)
        {
            if (IsASCIIPrintable(iInput))
                return ((char)iInput).ToString();
            else
                return " ";
        }

        public static bool IsASCIIPrintable(int iInput)
        {
            if (iInput < 32)
                return false;
            if (iInput > 126)
                return false;
            return true;
        }

    }
}
